<?php
namespace Liaiyong\Light\Core;

use Liaiyong\Light\Base\Singleton;
use Liaiyong\Light\Base\Serviceable;

abstract class Service extends Singleton implements Serviceable {
    public function get($id, $fields = array()) {
        return $this->model()->get($id, $fields);
    }
    public function add(array $info, $use_last_id = true) {
        return $this->model()->add($info, $use_last_id);
    }
    public function del($id, $verify = false) {
        return $this->model()->del($id, $verify);
    }
    public function upd($id, array $info, $verify = false) {
        return $this->model()->upd($id, $info, $verify);
    }
    public function count(array $info = array()) {
        return $this->model()->count($info);
    }
    public function replace(array $info = array()) {
        return $this->model()->replace($info);
    }
    // 增长字段值
    public function increase($field, $num = 1, $condition = array()) {
        return $this->model()->db()->increase($field, $num, $condition);
    }

    /**
     * unsafe
     * @param array $condition
     * @param array $order
     * @param array $limit
     */
    public function getAll($order = array(), $limit = array()) {
        return $this->model()->query(array(), array(), $order, $limit, false);
    }
    /**
     * unsafe, get list by sql
     * @param string $sql
     */
    public function querySQL($sql){
        return $this->query($sql);
    }
    /**
     * unsafe, get list by sql
     * @param string $sql
     */
    public function query($sql) {
        return $this->model()->querySQL($sql);
    }
    /**
     * unsafe, get count by sql
     * @param string $sql
     */
    public function queryCount($sql) {
        return $this->model()->queryCount($sql);
    }
    /**
     * query only by ids
     * @param array $ids
     */
    public function getList(array $ids = array()) {
        return $this->model()->lists($ids);
    }
    /**
     * safe
     * @param array $condition
     * @param array $order
     * @param array $limit
     */
    public function getSearchList($condition = array(), $order = array(), $limit = array()) {
        return $this->model()->search(array(), $condition, $order, $limit);
    }
    /**
     * 条件安全保护下的查询
     * @param array $condition
     * @param array $order
     * @param array $limit
     */
    public function getQueryList($condition = array(), $order = array(), $limit = array()) {
        return $this->model()->query(array(), $condition, $order, $limit);
    }
    /**
     * 条件非安全保护下的查询，非特殊原因请使用getQueryList()
     * @param array $condition
     * @param array $order
     * @param array $limit
     */
    public function getConditionList($condition = array(), $order = array(), $limit = array()) {
        return $this->model()->queryOne(array(), $condition, $order, $limit, false);
    }
    /**
     * 条件安全保护下的单条记录查询
     * @param array $condition
     * @param array $order
     * @param array $limit
     */
    public function getQueryOne($condition = array(), $order = array()) {
        return $this->model()->queryOne(array(), $condition, $order);
    }

    /**
     * set by unique fields' value
     */
    /*public function updByUnique($unique, array $info) {
        // not support
        return false;
    }*/
    /**
     * get by unique fields' value
     */
    /*public function getByUnique($unique) {
        $model = $this->model();
        if($model instanceof Uniqueness) {
            return $this->model()->getByUnique($unique);
        } else {
            return false;
        }
    }*/
    /**
     * delete by unique fields' value
     */
    /*public function delByUnique($unique) {
        // not support
        return false;
    }*/

    public function freeResult() {
        $db = $this->model()->db();
        if($db instanceof Mysql) {
            return $this->model()->db()->freeResult();
        } else {
            return true;
        }
    }
}
// PHP END
